﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TradeMeSDK;

namespace TradeMo
{
	public class CriteriaUsedCarsViewModel : BaseViewModel
	{
		private SearchUsedMotorsCriteria _criteria;
		public SearchUsedMotorsCriteria Criteria
		{
			get
			{
				if (_criteria == null)
				{
					_criteria = new SearchUsedMotorsCriteria();
					UpdateFields();
				}
				return _criteria;
			}
			set
			{
				_criteria = value;
				UpdateFields();
			}
		}



		private Locality _selectedRegion;
		public Locality SelectedRegion
		{
			get { return _selectedRegion; }
			set
			{
				_selectedRegion = value;
				NotifyPropertyChanged("RegionName");
				Criteria.Region = _selectedRegion != null ? _selectedRegion.LocalityId : 0;
			}
		}

		public string RegionName
		{
			get
			{
				return SelectedRegion == null ? "New Zealand" : SelectedRegion.Name;
			}
		}

		public List<SelectorItem<Locality>> Regions
		{
			get
			{
				if (!App.TradeMe.Catalog.IsDownloaded) return null;

				var list = new List<SelectorItem<Locality>>();
				list.Add(new SelectorItem<Locality> { Title = "New Zealand" });
				list.AddRange(
					App.TradeMe.Catalog.Areas.Select(area => new SelectorItem<Locality> { Title = area.Name, Item = area }));
				return list.ToList();
			}
		}

		////////////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedSortBy;
		public SelectorItem<string> SelectedSortBy
		{
			get
			{
				return _selectedSortBy ?? (_selectedSortBy = new SelectorItem<string> { Title = "Default" });
			}
			set
			{
				_selectedSortBy = value;
				NotifyPropertyChanged("SortByName");
				Criteria.SortOrder = _selectedSortBy != null ? _selectedSortBy.Item : null;
			}
		}
		public string SortByName
		{
			get { return SelectedSortBy.Title; }
			set { SelectedSortBy.Title = value; }
		}

		public string SortByItem
		{
			get { return Criteria.SortOrder; }
			set { Criteria.SortOrder = value; }
		}

		public List<SelectorItem<string>> SortByItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Title", Item = "TitleAsc"},
                        new SelectorItem<string> {Title = "Closing Soon", Item = "ExpiryAsc"},
                        new SelectorItem<string> {Title = "Latest Listings", Item = "ExpiryDesc"},
                        new SelectorItem<string> {Title = "Lowest Price", Item = "PriceAsc"},
                        new SelectorItem<string> {Title = "Highest Price", Item = "PriceDesc"},
                        new SelectorItem<string> {Title = "Most Bids", Item = "BidsMost"},
                        new SelectorItem<string> {Title = "Lowest Buy Now", Item = "BuyNowAsc"},
                        new SelectorItem<string> {Title = "Highest Buy Now", Item = "BuyNowDes"},
                    };

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedBodyStyle;
		public SelectorItem<string> SelectedBodyStyle
		{
			get
			{
				return _selectedBodyStyle ?? (_selectedBodyStyle = new SelectorItem<string> { Title = "Any body style" });
			}
			set
			{
				_selectedBodyStyle = value;
				NotifyPropertyChanged("BodyStyleName");
				Criteria.BodyStyle = _selectedBodyStyle != null ? _selectedBodyStyle.Item : null;
			}
		}
		public string BodyStyleName
		{
			get { return SelectedBodyStyle.Title; }
			set { SelectedBodyStyle.Title = value; }
		}
		public string BodyStyleItem
		{
			get { return Criteria.BodyStyle; }
			set { Criteria.BodyStyle = value; }
		}

		public List<SelectorItem<string>> BodyStyleItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item = "", Title = "Any body style"},
                        new SelectorItem<string> {Item = "Convertible", Title = "Convertible"},
                        new SelectorItem<string> {Item = "Coupe", Title = "Coupe"},
                        new SelectorItem<string> {Item = "Hatchback", Title = "Hatchback"},
                        new SelectorItem<string> {Item = "Sedan", Title = "Sedan"},
                        new SelectorItem<string> {Item = "Station Wagon", Title = "Station Wagon"},
                        new SelectorItem<string> {Item = "4x4 / SUV", Title = "4x4 / SUV"},
                        new SelectorItem<string> {Item = "Ute", Title = "Ute"},
                        new SelectorItem<string> {Item = "Van", Title = "Van"},
                    };

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedMake;
		public SelectorItem<string> SelectedMake
		{
			get
			{
				return _selectedMake ?? (_selectedMake = new SelectorItem<string> { Title = "Any make" });
			}
			set
			{
				_selectedMake = value;
				NotifyPropertyChanged("MakeName");
				Criteria.Make = _selectedMake != null ? _selectedMake.Item : null;
			}
		}
		public string MakeName
		{
			get { return SelectedMake.Title; }
			set { SelectedMake.Title = value; }
		}
		public string MakeItem
		{
			get { return Criteria.Make; }
			set { Criteria.Make = value; }
		}

		public List<SelectorItem<string>> MakeItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="", Title = "Any make"},


                        new SelectorItem<string> {Item="", Title = "Any make"},
                        new SelectorItem<string> {Item="Alfa romeo", Title = "Alfa romeo"},
                        new SelectorItem<string> {Item="Aston martin", Title = "Aston martin"},
                        new SelectorItem<string> {Item="Audi", Title = "Audi"},
                        new SelectorItem<string> {Item="Austin", Title = "Austin"},
                        new SelectorItem<string> {Item="Bentley", Title = "Bentley"},
                        new SelectorItem<string> {Item="BMW", Title = "BMW"},
                        new SelectorItem<string> {Item="Cadillac", Title = "Cadillac"},
                        new SelectorItem<string> {Item="Chevrolet", Title = "Chevrolet"},
                        new SelectorItem<string> {Item="Chrysler", Title = "Chrysler"},
                        new SelectorItem<string> {Item="Citroen", Title = "Citroen"},
                        new SelectorItem<string> {Item="Daewoo", Title = "Daewoo"},
                        new SelectorItem<string> {Item="Daihatsu", Title = "Daihatsu"},
                        new SelectorItem<string> {Item="Daimler", Title = "Daimler"},
                        new SelectorItem<string> {Item="Dodge", Title = "Dodge"},
                        new SelectorItem<string> {Item="Ferrari", Title = "Ferrari"},
                        new SelectorItem<string> {Item="Fiat", Title = "Fiat"},
                        new SelectorItem<string> {Item="Ford", Title = "Ford"},
                        new SelectorItem<string> {Item="Holden", Title = "Holden"},
                        new SelectorItem<string> {Item="Honda", Title = "Honda"},
                        new SelectorItem<string> {Item="Hummer", Title = "Hummer"},
                        new SelectorItem<string> {Item="Hyundai", Title = "Hyundai"},
                        new SelectorItem<string> {Item="Isuzu", Title = "Isuzu"},
                        new SelectorItem<string> {Item="Jaguar", Title = "Jaguar"},
                        new SelectorItem<string> {Item="Jeep", Title = "Jeep"},
                        new SelectorItem<string> {Item="Kia", Title = "Kia"},
                        new SelectorItem<string> {Item="Lamborghini", Title = "Lamborghini"},
                        new SelectorItem<string> {Item="Land Rover", Title = "Land Rover"},
                        new SelectorItem<string> {Item="Lexus", Title = "Lexus"},
                        new SelectorItem<string> {Item="Lotus", Title = "Lotus"},
                        new SelectorItem<string> {Item="Maserati", Title = "Maserati"},
                        new SelectorItem<string> {Item="Mazda", Title = "Mazda"},
                        new SelectorItem<string> {Item="Mercedes-Benz", Title = "Mercedes-Benz"},
                        new SelectorItem<string> {Item="MG", Title = "MG"},
                        new SelectorItem<string> {Item="Mini", Title = "Mini"},
                        new SelectorItem<string> {Item="Mitsubishi", Title = "Mitsubishi"},
                        new SelectorItem<string> {Item="Morris", Title = "Morris"},
                        new SelectorItem<string> {Item="Nissan", Title = "Nissan"},
                        new SelectorItem<string> {Item="Opel", Title = "Opel"},
                        new SelectorItem<string> {Item="Peugeot", Title = "Peugeot"},
                        new SelectorItem<string> {Item="Pontiac", Title = "Pontiac"},
                        new SelectorItem<string> {Item="Porsche", Title = "Porsche"},
                        new SelectorItem<string> {Item="Proton", Title = "Proton"},
                        new SelectorItem<string> {Item="Renault", Title = "Renault"},
                        new SelectorItem<string> {Item="Rolls-Royce", Title = "Rolls-Royce"},
                        new SelectorItem<string> {Item="Rover", Title = "Rover"},
                        new SelectorItem<string> {Item="Saab", Title = "Saab"},
                        new SelectorItem<string> {Item="Skoda", Title = "Skoda"},
                        new SelectorItem<string> {Item="Smart", Title = "Smart"},
                        new SelectorItem<string> {Item="Ssangyong", Title = "Ssangyong"},
                        new SelectorItem<string> {Item="Subaru", Title = "Subaru"},
                        new SelectorItem<string> {Item="Suzuki", Title = "Suzuki"},
                        new SelectorItem<string> {Item="Toyota", Title = "Toyota"},
                        new SelectorItem<string> {Item="Triumph", Title = "Triumph"},
                        new SelectorItem<string> {Item="Volkswagen", Title = "Volkswagen"},
                        new SelectorItem<string> {Item="Volvo", Title = "Volvo"},
					};

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedPriceFrom;
		public SelectorItem<string> SelectedPriceFrom
		{
			get
			{
				return _selectedPriceFrom ?? (_selectedPriceFrom = new SelectorItem<string> { Title = "Any" });
			}
			set
			{
				_selectedPriceFrom = value;
				NotifyPropertyChanged("PriceFromName");
				Criteria.MinPrice = _selectedPriceFrom.Item != null ? Convert.ToInt32(_selectedPriceFrom.Item) : 0;
			}
		}
		public string PriceFromName
		{
			get { return SelectedPriceFrom.Title; }
			set { SelectedPriceFrom.Title = value; }
		}

		private SelectorItem<string> _selectedPriceTo;
		public SelectorItem<string> SelectedPriceTo
		{
			get
			{
				return _selectedPriceTo ?? (_selectedPriceTo = new SelectorItem<string> { Title = "Any" });
			}
			set
			{
				_selectedPriceTo = value;
				NotifyPropertyChanged("PriceToName");
				Criteria.MaxPrice = _selectedPriceTo != null ? Convert.ToInt32(_selectedPriceTo.Item) : 0;
			}
		}
		public string PriceToName
		{
			get { return SelectedPriceTo.Title; }
			set { SelectedPriceTo.Title = value; }
		}

		public List<SelectorItem<string>> PriceItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="2000", Title = "$2k"},
                        new SelectorItem<string> {Item="4000", Title = "$4k"},
						new SelectorItem<string> {Item="6000", Title = "$6k"},
                        new SelectorItem<string> {Item="8000", Title = "$8k"},
                        new SelectorItem<string> {Item="10000", Title = "$10k"},
                        new SelectorItem<string> {Item="12000", Title = "$12k"},
                        new SelectorItem<string> {Item="15000", Title = "$15k"},
                        new SelectorItem<string> {Item="20000", Title = "$20k"},
                        new SelectorItem<string> {Item="25000", Title = "$25k"},
                        new SelectorItem<string> {Item="30000", Title = "$30k"},
                        new SelectorItem<string> {Item="40000", Title = "$40k"},
                    };

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedYearFrom;
		public SelectorItem<string> SelectedYearFrom
		{
			get
			{
				return _selectedYearFrom ?? (_selectedYearFrom = new SelectorItem<string> { Title = "Any" });
			}
			set
			{
				_selectedYearFrom = value;
				NotifyPropertyChanged("YearFromName");
				Criteria.MinYear = _selectedYearFrom.Item != null ? Convert.ToInt32(_selectedYearFrom.Item) : 0;
			}
		}
		public string YearFromName
		{
			get { return SelectedYearFrom.Title; }
			set { SelectedYearFrom.Title = value; }
		}

		private SelectorItem<string> _selectedYearTo;
		public SelectorItem<string> SelectedYearTo
		{
			get
			{
				return _selectedYearTo ?? (_selectedYearTo = new SelectorItem<string> { Title = "Any" });
			}
			set
			{
				_selectedYearTo = value;
				NotifyPropertyChanged("YearToName");
				Criteria.MaxYear = _selectedYearTo != null ? Convert.ToInt32(_selectedYearTo.Item) : 0;
			}
		}
		public string YearToName
		{
			get { return SelectedYearTo.Title; }
			set { SelectedYearTo.Title = value; }
		}

		public List<SelectorItem<string>> YearItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="", Title = "Any"},
                        new SelectorItem<string> {Item="2010", Title = "2010"},
                        new SelectorItem<string> {Item="2008", Title = "2008"},
						new SelectorItem<string> {Item="2006", Title = "2006"},
                        new SelectorItem<string> {Item="2004", Title = "2004"},
                        new SelectorItem<string> {Item="2002", Title = "2002"},
                        new SelectorItem<string> {Item="2000", Title = "2000"},
                        new SelectorItem<string> {Item="1998", Title = "1998"},
                        new SelectorItem<string> {Item="1996", Title = "1996"},
                        new SelectorItem<string> {Item="1994", Title = "1994"},
                        new SelectorItem<string> {Item="1992", Title = "1992"},
                        new SelectorItem<string> {Item="1990", Title = "1990"},
                        new SelectorItem<string> {Item="1980", Title = "1980"},
                        new SelectorItem<string> {Item="1970", Title = "1970"},
                        new SelectorItem<string> {Item="1960", Title = "1960"},
                        new SelectorItem<string> {Item="1950", Title = "1950"},
                        new SelectorItem<string> {Item="1900", Title = "1900"},
                    };

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////


		private void UpdateFields()
		{
			NotifyPropertyChanged("Criteria");

			if (Criteria.Region != 0)
				SelectedRegion = App.TradeMe.Catalog.Areas.FirstOrDefault(l => l.LocalityId == Criteria.Region);

			if (!string.IsNullOrEmpty(Criteria.SortOrder))
				SelectedSortBy = SortByItems.FirstOrDefault(s => s.Item == Criteria.SortOrder);

			if (!string.IsNullOrEmpty(Criteria.BodyStyle))
				SelectedBodyStyle = BodyStyleItems.FirstOrDefault(bs => bs.Item == Criteria.BodyStyle);

			if (!string.IsNullOrEmpty(Criteria.Make))
				SelectedMake = MakeItems.FirstOrDefault(m => m.Item == Criteria.Make);

			if (Criteria.MinPrice > 0)
			{
				string price = ((int)Criteria.MinPrice).ToString();
				SelectedPriceFrom = PriceItems.FirstOrDefault(p => p.Item == price);
			}
			if (Criteria.MaxPrice > 0)
			{
				string price = ((int)Criteria.MaxPrice).ToString();
				SelectedPriceTo = PriceItems.FirstOrDefault(p => p.Item == price);
			}

			if (Criteria.MinYear > 0)
			{
				string year = Criteria.MinYear.ToString();
				SelectedYearFrom = YearItems.FirstOrDefault(p => p.Item == year);
			}
			if (Criteria.MaxYear > 0)
			{
				string year = Criteria.MaxYear.ToString();
				SelectedYearTo = YearItems.FirstOrDefault(p => p.Item == year);
			}

		}

	    public void Reset()
	    {
            SelectedRegion = null;
            SelectedSortBy = null;
	        SelectedBodyStyle = null;
	        SelectedMake = null;
            SelectedPriceFrom = null;
            SelectedPriceTo = null;
	        SelectedYearFrom = null;
	        SelectedYearTo = null;
            UpdateFields();
	    }
	}
}
